package com.abc.pay.client;

import com.sun.net.ssl.internal.ssl.Provider;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public abstract class MerchantPara {
    private List MerchantIDList = new ArrayList();
    private List MerchantCertificateList = new ArrayList();
    private List MerchantKeyList = new ArrayList();
    private String TrustPayConnectMethod = null;
    private String TrustPayServerName = null;
    private String TrustPayServerPort = null;
    private String TrustPayConnectMethodLine = null;
    private String TrustPayServerNameLine = null;
    private String TrustPayServerPortLine = null;
    private String TrustPayTrxURL = null;
    private String TrustPayFileTrxURL = null;
    private String TrustPayTrxIEURL = null;
    private String MerchantErrorURL = null;
    private byte[] TrustPayCertFile = null;
    private String TrustPayCertFileName = null;
    private boolean PrintLog = false;
    private String LogPath = null;
    private ArrayList<byte[]> MerchantCertFileList = null;
    private ArrayList<String> MerchantCertFileListString = null;
    private ArrayList<String> MerchantCertPasswordList = null;
    private String ProxyIP = "";
    private String ProxyPort = "";
    private String TrustPayServerTimeout = "";
    private String SignServerIP = "";
    private String SignServerPort = "";
    private String SignServerPassword = "";

    public MerchantPara() {
    }

    public abstract void refreshConfig();

    public String getTrustPayConnectMethod() {
        return this.TrustPayConnectMethod;
    }

    public void setTrustPayConnectMethod(String trustPayConnectMethod) {
        this.TrustPayConnectMethod = trustPayConnectMethod;
    }

    public String getTrustPayServerName() {
        return this.TrustPayServerName;
    }

    public void setTrustPayServerName(String trustPayServerName) {
        this.TrustPayServerName = trustPayServerName;
    }

    public String getTrustPayServerPort() {
        return this.TrustPayServerPort;
    }

    public void setTrustPayServerPort(String trustPayServerPort) {
        this.TrustPayServerPort = trustPayServerPort;
    }

    public String getTrustPayTrxURL() {
        return this.TrustPayTrxURL;
    }

    public void setTrustPayTrxURL(String trustPayTrxURL) {
        this.TrustPayTrxURL = trustPayTrxURL;
    }

    public String getTrustPayFileTrxURL() {
        return this.TrustPayFileTrxURL;
    }

    public void setTrustPayFileTrxURL(String trustPayFileTrxURL) {
        this.TrustPayFileTrxURL = trustPayFileTrxURL;
    }

    public String getTrustPayTrxIEURL() {
        return this.TrustPayTrxIEURL;
    }

    public void setTrustPayTrxIEURL(String trustPayTrxIEURL) {
        this.TrustPayTrxIEURL = trustPayTrxIEURL;
    }

    public byte[] getTrustPayCertFile() {
        return this.TrustPayCertFile;
    }

    public String getTrustPayCertFileName() {
        return this.TrustPayCertFileName;
    }

    public void setTrustPayCertFileName(String trustPayCertFile) {
        this.TrustPayCertFileName = trustPayCertFile;
    }

    public void setTrustPayCertFile(byte[] trustPayCertFile) {
        this.TrustPayCertFile = trustPayCertFile;
    }

    public String getLogPath() {
        return this.LogPath;
    }

    public void setLogPath(String logPath) {
        this.LogPath = logPath;
    }

    public ArrayList<byte[]> getMerchantCertFileList() {
        return this.MerchantCertFileList;
    }

    public boolean isPrintLog() {
        return this.PrintLog;
    }

    public void setPrintLog(boolean printLog) {
        this.PrintLog = printLog;
    }

    public void setMerchantCertFileList(ArrayList<byte[]> merchantCertFileList) {
        this.MerchantCertFileList = merchantCertFileList;
    }

    public ArrayList<String> getMerchantCertFileListString() {
        return this.MerchantCertFileListString;
    }

    public void setMerchantCertFileListString(ArrayList<String> merchantCertFileList) {
        this.MerchantCertFileListString = merchantCertFileList;
    }

    public ArrayList<String> getMerchantCertPasswordList() {
        return this.MerchantCertPasswordList;
    }

    public void setMerchantCertPasswordList(ArrayList<String> merchantCertPasswordList) {
        this.MerchantCertPasswordList = merchantCertPasswordList;
    }

    public String getSignServerIP() {
        return this.SignServerIP;
    }

    public void setSignServerIP(String signServerIP) {
        this.SignServerIP = signServerIP;
    }

    public String getSignServerPort() {
        return this.SignServerPort;
    }

    public void setSignServerPort(String signServerPort) {
        this.SignServerPort = signServerPort;
    }

    public String getProxyIP() {
        return this.ProxyIP;
    }

    public void setProxyIP(String proxyIP) {
        this.ProxyIP = proxyIP;
    }

    public String getProxyPort() {
        return this.ProxyPort;
    }

    public void setProxyPort(String proxyPort) {
        this.ProxyPort = proxyPort;
    }

    public String getTrustPayServerTimeout() {
        return this.TrustPayServerTimeout;
    }

    public void setTrustPayServerTimeout(String trustPayServerTimeout) {
        this.TrustPayServerTimeout = trustPayServerTimeout;
    }

    public String getSignServerPassword() {
        return this.SignServerPassword;
    }

    public void setSignServerPassword(String signServerPassword) {
        this.SignServerPassword = signServerPassword;
    }

    public String getTrustPayConnectMethodLine() {
        return this.TrustPayConnectMethodLine;
    }

    public void setTrustPayConnectMethodLine(String trustPayConnectMethodLine) {
        this.TrustPayConnectMethodLine = trustPayConnectMethodLine;
    }

    public String getTrustPayServerNameLine() {
        return this.TrustPayServerNameLine;
    }

    public void setTrustPayServerNameLine(String trustPayServerNameLine) {
        this.TrustPayServerNameLine = trustPayServerNameLine;
    }

    public String getTrustPayServerPortLine() {
        return this.TrustPayServerPortLine;
    }

    public void setTrustPayServerPortLine(String trustPayServerPortLine) {
        this.TrustPayServerPortLine = trustPayServerPortLine;
    }

    public List getMerchantIDList() {
        return this.MerchantIDList;
    }

    public void setMerchantIDList(List merchantIDList) {
        this.MerchantIDList = merchantIDList;
    }

    public List getMerchantCertificateList() {
        return this.MerchantCertificateList;
    }

    public void setMerchantCertificateList(List merchantCertificateList) {
        this.MerchantCertificateList = merchantCertificateList;
    }

    public List getMerchantKeyList() {
        return this.MerchantKeyList;
    }

    public void setMerchantKeyList(List merchantKeyList) {
        this.MerchantKeyList = merchantKeyList;
    }

    public String getMerchantID(int i) {
        return (String)this.MerchantIDList.get(i - 1);
    }

    public String getMerchantCertificate(int i) {
        return (String)this.MerchantCertificateList.get(i - 1);
    }

    public PrivateKey getMerchantKey(int i) {
        return (PrivateKey)this.MerchantKeyList.get(i - 1);
    }

    public Certificate getTrustpayCertificate() throws TrxException {
        return getCertificate(this.TrustPayCertFileName);
    }

    protected static Certificate getCertificate(String tCertFile) throws TrxException {
        Certificate tCertificate = null;
        byte[] tCertBytes = new byte[8192];
        InputStream tIn = null;
        try {
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            if(contextClassLoader != null) {
                tIn = contextClassLoader.getResourceAsStream(tCertFile);
                if (tIn != null) {
                    int tCertBytesLen = tIn.read(tCertBytes);
                    if(tCertBytesLen > 0) {
                        byte[] tFinalCertBytes = new byte[tCertBytesLen];
                        System.arraycopy(tCertBytes, 0, tFinalCertBytes, 0, tCertBytesLen);
                        Security.addProvider(new Provider());
                        CertificateFactory tCertificateFactory = CertificateFactory.getInstance("X.509");
                        ByteArrayInputStream bais = new ByteArrayInputStream(tFinalCertBytes);
                        if (bais.available() > 0) {
                            tCertificate = tCertificateFactory.generateCertificate(bais);
                        }
                    }
                } else {
                    throw new TrxException("1002", "无法读取证书文档[" + tCertFile + "] tIn != null");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new TrxException("1002", "无法读取证书文档[" + tCertFile + "]");
        } finally {
            try {
                if(tIn != null) {
                    tIn.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        return tCertificate;
    }

    public String getMerchantErrorURL() {
        return this.MerchantErrorURL;
    }

    public void setMerchantErrorURL(String merchantErrorURL) {
        this.MerchantErrorURL = merchantErrorURL;
    }

}